package com.yungu.swift.base;

import java.io.Serializable;

/**
 * @author cuixiuyin
 */
public class ResponseData<T> implements Serializable {

    public static final Integer SUCCESS_CODE = 1;
    public static final Integer ERROR_CODE = 0;

    private int errCode;
    private long ts;
    private String msg;

    private T data;

    private ResponseData() {
    }

    private static <S> ResponseData<S> buildResponse(int code, String msg, S data) {
        ResponseData<S> resp = new ResponseData<>();
        resp.setErrCode(code);
        resp.setMsg(msg);
        resp.setTs(System.currentTimeMillis() / 1000);
        resp.setData(data);
        return resp;
    }

    public static ResponseData buildErrorResponse(int errCode, String errMsg) {
        return buildResponse(errCode, errMsg, null);
    }

    public static <S> ResponseData<S> buildErrorResponse(int errCode, String errMsg, S result) {
        return buildResponse(errCode, errMsg, result);
    }

    public static <S> ResponseData<S> buildSuccessResponse(S result) {
        return buildResponse(SUCCESS_CODE, "请求成功", result);
    }

    public static <S> ResponseData<S> buildSuccessResponse(String msg, S result) {
        return buildResponse(SUCCESS_CODE, msg, result);
    }

    public Boolean isSuccess() {
        return errCode == SUCCESS_CODE;
    }

    public int getErrCode() {
        return errCode;
    }

    public void setErrCode(int errCode) {
        this.errCode = errCode;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public long getTs() {
        return ts;
    }

    public void setTs(long ts) {
        this.ts = ts;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
